package cn.bgs.controller;
import cn.bgs.pojo.curriculum_subject;
import cn.bgs.pojo.trimesterCurriculumSchedule;
import cn.bgs.service.curriculumService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @ProjectName: bei
 * @Package: cn.bgs.controller
 * @ClassName: CurriculumController
 * @Author: Administrator
 * @Description:
 * @Date: 2021/8/26 0026 16:01
 * @Version: 1.0
 */
@Controller
public class CurriculumController {
    @Autowired
    private curriculumService curriculumService;

   @RequestMapping("/curriculumHtml")
    public String curriculumHtml(){
       return "curriculum";
   }
    @ResponseBody
    @RequestMapping("/findCurriculum")
    public Map<String,Object> findCurriculum(@RequestParam(required=false,defaultValue="1") int page,
                                                     @RequestParam(required=false,defaultValue="7") int limit,
                                                     curriculum_subject curriculumSubject){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<curriculum_subject> curriculum  = curriculumService.findCurriculum(curriculumSubject);// 这是我们的sql
        // 使用pageInfo包装查询
        PageInfo<curriculum_subject> rolePageInfo = new PageInfo<>(curriculum);//
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        return map;
    }
    @RequestMapping("/insertCurri")
    @ResponseBody
    public Integer insertCurri(@RequestBody curriculum_subject curriculumSubject){
        curriculumSubject.setDeleteFlag("0");
        curriculumSubject.setCreator("张三");
        Calendar now = Calendar.getInstance();//获取时间
        curriculumSubject.setCreateTime(now.getTime());
        curriculumSubject.setUpdateTime(now.getTime());//当前时间
       return curriculumService.insertCurri(curriculumSubject);
    }
    @RequestMapping("/deleteCur")
    @ResponseBody
    public Integer deleteCur(@RequestBody String jsonStr){

        JSONObject jsonObject=JSONObject.parseObject(jsonStr);
        JSONArray ids=jsonObject.getJSONArray("pkIds");
        Integer[] pkIds=new Integer[ids.size()];
        for(int i=0;i<ids.size();i++){
            Object id = ids.get(i);
            pkIds[i]=Integer.parseInt(id.toString());
        }
        List<Integer> list= Arrays.asList(pkIds);

        return curriculumService.deleteCur(list);
    }
    @RequestMapping("/curriDelete")
    @ResponseBody
    public Integer curriDelete(@RequestBody String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer pkId=Integer.parseInt(jsonObject.getString("pkId"));
        return curriculumService.curriDelete(pkId);
    }
    @RequestMapping("/updateCurric")
    @ResponseBody
    public Integer updateCurric(@RequestBody curriculum_subject curriculumSubject){

        curriculumSubject.setDeleteFlag("0");
        curriculumSubject.setCreator("张三");
        Calendar now = Calendar.getInstance();//获取时间
        curriculumSubject.setCreateTime(now.getTime());
        curriculumSubject.setUpdateTime(now.getTime());//当前时间
        return curriculumService.updateCurric(curriculumSubject);
    }
    @RequestMapping("/trimesterFind/{gradeName}")
    public String trimesterFind(@PathVariable String gradeName){
       return "/trimester";
    }
    @RequestMapping("/trimester")
    @ResponseBody
    public List<trimesterCurriculumSchedule> trimester(){
        List<trimesterCurriculumSchedule> list = curriculumService.findTrimester();
        String week = "";
//        int a= 1,b=2;
//        a = (a+b)-a;
//        b = (a+b)-b;
//        System.out.println(a);
//        System.out.println(b);
        for(int i = 0 ;i<list.size();i++){
                Date today = list.get(i).getBookDate();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(today);
                int weekday = calendar.get(Calendar.DAY_OF_WEEK);
                if (weekday == 1) {
                    week = "星期日";
                } else if (weekday == 2) {
                    week = "星期一";
                } else if (weekday == 3) {
                    week = "星期二";
                } else if (weekday == 4) {
                    week = "星期三";
                } else if (weekday == 5) {
                    week = "星期四";
                } else if (weekday == 6) {
                    week = "星期五";
                } else if (weekday == 7) {
                    week = "星期六";
                }
                System.out.println("周几+++++"+  week);
                list.get(i).setWeek(week);
            }
        return list;
    }
}
